package com.terrorism.sdk.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.terrorism.sdk.R;

/**
 * @since Wednesday, June 25, 2014
 * @author hoangcaomobile
 *
 */
public class OptionAdapter extends ArrayAdapter<OptionItem> {

	private List<OptionItem> arrayList = new ArrayList<OptionItem>();

	@Override
	public void add(OptionItem object) {
		arrayList.add(object);
		super.add(object);
	}

	public OptionAdapter(Activity a) {
		super(a, 0);
	}

	public int getCount() {
		return this.arrayList.size();
	}

	public OptionItem getItem(int index) {
		return this.arrayList.get(index);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v = inflater.inflate(R.layout.ui_list_item_option, parent, false);

		RelativeLayout layoutTitle = (RelativeLayout) v.findViewById(R.id.layoutTitle);
		RelativeLayout layoutItem = (RelativeLayout) v.findViewById(R.id.layoutItem);
		TextView tvTitle = (TextView) v.findViewById(R.id.tvTitle);
		TextView tvItem = (TextView) v.findViewById(R.id.tvItem);

		OptionItem item = getItem(position);

		layoutTitle.setVisibility(item.isTitle ? View.VISIBLE : View.GONE);
		layoutItem.setVisibility(item.isTitle ? View.GONE : View.VISIBLE);
		layoutItem.setBackgroundResource(item.isSelected ? R.drawable.list_selector2 : R.drawable.list_selector);
		tvTitle.setText(item.title);
		tvItem.setText(item.title);

		return v;
	}

	public Bitmap decodeToBitmap(byte[] decodedByte) {
		return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
	}
}